package com.wuhaha.a1911_demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.wuhaha.a1911_demo.R;
import com.wuhaha.a1911_demo.model.BottomBar;
import com.wuhaha.a1911_demo.model.Destination;
import com.wuhaha.a1911_demo.utlis.AppConfig;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public
/**
 * Create date: On 2022/8/15
 */
class AppBottomBar extends BottomNavigationView {
    private static int[] sIcons = new int[]{
            R.drawable.icon_tab_home,
            R.drawable.icon_tab_sofa,
            R.drawable.icon_tab_publish,
            R.drawable.icon_tab_find,
            R.drawable.icon_tab_mine
    };

    private BottomBar config;

    public AppBottomBar(@NonNull @NotNull Context context) {
        this(context,null);
    }

    public AppBottomBar(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    @SuppressLint("RestrictedApi")
    public AppBottomBar(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        BottomBar bottomBarConfig = AppConfig.getBottomBarConfig();
        List<BottomBar.TabsDTO> tabs = bottomBarConfig.tabs;
        //选中和未被选中状态下的颜色
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_selected};
        states[1] = new int[]{};
        //给按钮设置选中的颜色
        int[] colors = new int[]{Color.parseColor(bottomBarConfig.activeColor),Color.parseColor(bottomBarConfig.inActiveColor)};
        ColorStateList colorStateList = new ColorStateList(states,colors);
        setItemIconTintList(colorStateList);
        setItemTextColor(colorStateList);
        setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);//设置模式  可以设置按钮的文本显示还是不显示，比如：选中时显示

        //设置默认选中的按钮
        setSelectedItemId(bottomBarConfig.selectTab);
        for (int i = 0;i < tabs.size();i++){
            BottomBar.TabsDTO tabsDTO = tabs.get(i);
            if (!tabsDTO.enable) {
                return;
            }
            int id = getId(tabsDTO.pageUrl);
            if (id < 0){
                return;
            }
            MenuItem item = getMenu().add(0, id, tabsDTO.index, tabsDTO.title);
            item.setIcon(sIcons[tabsDTO.index]);
        }

        for (int i = 0; i < tabs.size(); i++) {
            BottomBar.TabsDTO tabsDTO = tabs.get(i);
            int iconSize = dp2px(tabsDTO.size);

            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(tabsDTO.index);
            itemView.setIconSize(iconSize);

            if (TextUtils.isEmpty(tabsDTO.title)){
                itemView.setIconTintList(ColorStateList.valueOf(Color.parseColor(tabsDTO.tintColor)));
                itemView.setShifting(false);
            }
        }
    }

    private int dp2px(int size) {
        float valueNum = getContext().getResources().getDisplayMetrics().density * size + 0.5f;
        return (int) valueNum;
    }

    private int getId(String pageUrl) {
        Destination destination = AppConfig.getsDestConfig().get(pageUrl);
        if (destination == null){
            return -1;
        }
        return destination.getId();
    }
}
